package com.sl.plugin.step.json;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sl.core.domain.dto.JsonMappingFieldDefine;
import com.sl.core.engine.meta.ProcessCompDescriptorService;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.meta.row.ProcessRowDescriptor;
import com.sl.core.engine.transfer.JoltSpecGenerator;
import com.sl.core.utils.JsonUtil;
import lombok.Getter;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Getter
public class JsonOutputCompDescriptor extends AbstractProcessCompDescriptor<
        JsonOutputComp, JsonOutputCompContext> implements ProcessCompDescriptorService {


//    @Override
//    protected ProcessCompService getComp() {
//        return new JsonOutputComp(this);
//    }
//
//    @Override
//    public ProcessCompContextService getContext() {
//        return new JsonOutputCompContext();
//    }


    @Override
    public void loadDescriptor(Map<String, Object> map) {
        Map<String, Object> maps = MapUtil.get(map, "output", Map.class);
        List<Map<String, Object>> params = getListMapAttr(maps, "params");
        //找到对象关系
        List<JsonMappingFieldDefine> jsonMappingFieldDefine = JsonUtil.toBean(params, new TypeReference<List<JsonMappingFieldDefine>>() {
        });

        if (CollUtil.isNotEmpty(jsonMappingFieldDefine)) {
            //转移到了映射
            String transfer = JoltSpecGenerator.transfer(jsonMappingFieldDefine);
            putAttr("jolt", transfer);
        }
        buildResultProcessRowDescriptor(maps);
        //获得映射关系
        super.setIsEndStep(true);
    }


    @Override
    public ProcessRowDescriptor getResultDescriptor(ProcessRowDescriptor rowDescriptor) {
        ProcessRowDescriptor resultRowDescriptor = getResultRowDescriptor();
        if (ObjectUtil.isNotNull(resultRowDescriptor)) {
            return resultRowDescriptor;
        }

        return rowDescriptor;
    }

}
